package org.example.mymq.mqserver.datacenter;

import org.example.mymq.MyMqApplication;
import org.example.mymq.mqserver.core.Binding;
import org.example.mymq.mqserver.core.Exchange;
import org.example.mymq.mqserver.core.ExchangeType;
import org.example.mymq.mqserver.core.MSGQueue;
import org.example.mymq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

public class DataBaseManager {
    private MetaMapper metaMapper;
    //    针对数据库，初始化
    public void init(){
//        手动获取dataBaseManager
        metaMapper = MyMqApplication.context.getBean(MetaMapper.class);

        if (!checkDBExists()){
//        是否存在数据库，不存在则建表
//        创建数据表
//        需要先创建一个data目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            createTable();
//            插入默认数据
            createDefaultData();
            System.out.println("【DataBaseManager】数据库初始化完成！");
        }else {
            System.out.println("【DataBaseManager】数据库已经存在！");
        }
    }

    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 删除数据库文件成功!");
        } else {
            System.out.println("[DataBaseManager] 删除数据库文件失败!");
        }

//        删除目录
        File dataDir = new File("./data");
//        delete删除目录时，必须保证目录里面是空的
        ret = dataDir.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 删除数据库目录成功!");
        } else {
            System.out.println("[DataBaseManager] 删除数据库目录失败!");
        }
    }

    private boolean checkDBExists() {
//        url: jdbc:sqlite:./data/meta.db
//        判断数据库是否存在，就是判断是否存在meta.db文件
        File file = new File("./data/meta.db");
        if (file.exists()){
            return true;
        }
        return false;
    }

    //    meta.db文件MyBatis会自动帮我们生成
//    建表
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("【DataBaseManager】创建表完成");
    }

    //    给数据库表，添加默认数据
//    添加默认交换机
    private void createDefaultData() {
//        构造一个默认的交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("【DataBaseManager】创建初识数据完成。");
    }

    //    其他类数据库的操作
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }



    public void insertQueue(MSGQueue queue) {
        metaMapper.insertQueue(queue);
    }

    public List<MSGQueue> selectAllQueue(){
        return metaMapper.selectAllQueues();
    }

    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }



    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public List<Binding> selectAllBinding(){
        return metaMapper.selectAllBindings();
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }
}
